package com.travel.server.manager.impl.command.customer.data;

import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.server.manager.dto.request.PageRequest;
import com.travel.server.manager.dto.request.customer.data.QueryProjectCustomerAnalysisRequest;
import com.travel.server.manager.dto.response.customer.data.QueryProjectCustomerAnalysisResponse;
import com.travel.server.manager.dto.response.customer.data.QueryProjectCustomerAnalysisResponse.AnalysisItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.CustomerDal;
import com.travel.server.manager.model.dto.data.ProjectCustomerDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/10 16:24
 * @Desc:
 */
@Component
@Slf4j
public class QueryProjectCustomerAnalysisCommand extends AbstractCommand<QueryProjectCustomerAnalysisRequest, QueryProjectCustomerAnalysisResponse> {

    @Autowired
    private CustomerDal customerDal;

    @Override
    protected void checkInput(QueryProjectCustomerAnalysisRequest request) {
        checkPageInput(request);
    }

    @Override
    public QueryProjectCustomerAnalysisResponse handle(QueryProjectCustomerAnalysisRequest request) {
        checkInput(request);
        QueryProjectCustomerAnalysisResponse response = new QueryProjectCustomerAnalysisResponse();

        Integer totalNum = customerDal.selectProjectCustomerCount();

        response.setTotalNum(totalNum);
        response.setAnalysisList(makeList(request));
        return response;
    }

    public List<AnalysisItem> makeList(PageRequest request){
        List<AnalysisItem> analysisItemList = new ArrayList<>();
        List<ProjectCustomerDto> list = customerDal.selectProjectCustomer(request.getOffset(), request.getLimit());
        if(!CollectionUtil.isEmpty(list)){
            for(ProjectCustomerDto dto : list){
                AnalysisItem item = new AnalysisItem();
                item.setProject(dto.getName());
                int total95 = dto.getMan95() + dto.getWoman95();
                int total90 = dto.getMan90() + dto.getWoman90();
                int total85 = dto.getMan85() + dto.getWoman85();
                int total80 = dto.getMan80() + dto.getWoman80();

                int[] numbers = {total80, total85, total90, total95};
                int max = bubbleSort(numbers);
                if(max == total95){
                    item.setAge("95后");
                    item.setMan(CommonUtil.getProportion(dto.getMan95(), total95));
                    item.setWoman(CommonUtil.getProportion(dto.getWoman95(), total95));
                }else if(max == total90){
                    item.setAge("90后");
                    item.setMan(CommonUtil.getProportion(dto.getMan90(), total90));
                    item.setWoman(CommonUtil.getProportion(dto.getWoman90(), total90));
                }else if(max == total85){
                    item.setAge("85后");
                    item.setMan(CommonUtil.getProportion(dto.getMan85(), total85));
                    item.setWoman(CommonUtil.getProportion(dto.getWoman85(), total85));
                }else{
                    item.setAge("80后");
                    item.setMan(CommonUtil.getProportion(dto.getMan80(), total80));
                    item.setWoman(CommonUtil.getProportion(dto.getWoman80(), total80));
                }
                analysisItemList.add(item);
            }
        }
        return analysisItemList;
    }

    public static int bubbleSort(int[] a) {
        int temp;
        int size = a.length;
        for(int i=1; i<size; i++) {
            for(int j=0; j<size-i; j++) {
                if(a[j] < a[j+1]) {
                    temp = a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
        return a[0];
    }
}
